import { formatDataTime, YmdFormt } from './DateUtil'
import { formatNullStr, isObjectNull } from './EmptyUtil'
import { fixFloat, formatNumber } from './NumberUtil'

const defaultProps={ nameKey: 'name', inputKey: 'serverName' }

/**
 * 从对象中获取字段
 * @param {*} key
 * @param {*} data
 * @returns
 */
export function getPropValue (key, data,defaultValue) {
  if (key && key.length > 0) {
    const array = key.split('.')
    if (array && array.length > 0) {
      let obj = data
      let i = 0
      while (obj && i < array.length) {
        obj = obj[array[i]]
        i++
      }
      if(defaultValue||defaultValue===0)
      {
        return getValue(obj,defaultValue)
      }
      return obj
    }
  }
  return null
}

export function getValue(value, defaultValue) {
  
  if(value==="")
  {
    if(!defaultValue)
    {
      return defaultValue;
    }
    else{
      return value
    }
  }
  else if (value != null && value != undefined) {
    return value
  } 
  return defaultValue
}

/**
 * 删除object里面的某些字段
 * @param {*} item
 * @param {*} keys
 */
export function deleteObjKeys(item, keys) {
  if (!item || !keys || keys.length === 0) {
    return
  }
  for (let i = 0; i < keys.length; i++) {
    delete item[keys[i]]
  }
}

/**
 * 按照模板转换单个字段
 * @param {*} inputData 输入数据
 * @param {*} setItem  转换的模板
 * @param {*} output 输出数据
 * @param {*} props 输入和输出字段的配置  默认值为{nameKey:'name',inputKey:"serverName"}
 */
function formatItemFormSet(inputData, item, output = {}, props = { nameKey: 'name', inputKey: 'serverName' }) {
  const name = item[props.nameKey]
  const serverName = item[props.inputKey] || name
  const defaultValue = item.defaultValue
  if (!name) {
    return
  }
  if (typeof serverName === 'function') {
    serverName(inputData, item, output)
  } else if (typeof serverName === 'string') {
    const value = getPropValue(serverName, inputData)
    output[name] = getValue(value, defaultValue)
  }else if (Array.isArray(serverName)) {
    const array = [];
    for(let i=0;i<serverName.length;i++)
    {
      const value = getPropValue(serverName[i], inputData)
      array.push(value)
    }
    output[name] = array
  } else if (typeof serverName === 'object') {
    const { type } = serverName
    const inputName = serverName[props.inputKey] || name

    if (type === 'number') {
      const { digit,round } = serverName
      const value = getPropValue(inputName, inputData)
      if (!isObjectNull(digit)) {
        output[name] = fixFloat(value, digit, round)
      } else {
        output[name] = formatNumber(value, defaultValue)
      }
    } else if (type === 'string') {
      const value = `${formatNullStr(getPropValue(inputName, inputData))}`
      output[name] = getValue(value, defaultValue)
    } else if (type === 'date') {
      const value = formatDataTime(getPropValue(inputName, inputData), serverName.format || YmdFormt)
      output[name] = getValue(value, defaultValue)
    } else if (type === 'object') {
      const inputSet = serverName.set
      output[name] = formatDataFromSet(inputData, inputSet, {}, serverName.props || props)
    } else if (type === 'array') {
      const inputSet = serverName.set
      const {newData,deleteKeys}=serverName
      const inputArray = getPropValue(inputName, inputData)
      const outputArray = []
      for (let j = 0; j < inputArray.length; j++) {
        const inputItem = inputArray[j]
        const outputItem = newData?{}:{...inputItem}
        for (let k = 0; k < inputSet.length; k++) {
          const setItem = inputSet[k]
          const fromParent = item.fromParent
          if (fromParent) {
            formatItemFormSet(inputData, setItem, outputItem, serverName.props || props)
          } else {
            formatItemFormSet(inputItem, setItem, outputItem, serverName.props || props)
          }
        }
        deleteObjKeys(outputItem,deleteKeys)
        outputArray.push(outputItem)
      }
      output[name] = outputArray
    }
  }
}

/**
 * 按照模板转换对象
 * @param {*} inputData  输入数据
 * @param {*} set        转换的模板
 * @param {*} output     输出数据
 * @param {*} props      输入和输出字段的配置  默认值为{nameKey:'name',inputKey:"serverName"}
 */
export function formatDataFromSet(inputData, set, output, props ) {
  output=output||{}
  props=props||defaultProps
  if (!set || set.length == 0) {
    return output
  }
  for (let i = 0; i < set.length; i++) {
    const item = set[i]
    formatItemFormSet(inputData, item, output, props)
  }
  return output
}

/**
 * 按照模板转换数组
 * @param {*} inputData  输入数组
 * @param {*} set        转换的模板
 * @param {*} output     输出数组
 * @param {*} props      输入和输出字段的配置  默认值为{nameKey:'name',inputKey:"serverName"}
 */
 export function formatArrayFromSet(inputArray, set, outputArray, props = defaultProps) {
  outputArray = outputArray || []
  if (!inputArray || inputArray.length === 0) {
    return outputArray
  }
  if (!set) {
    return outputArray
  }
  let setList = set
  let newData
  let deleteKeys
  if (set&&!Array.isArray(set)) {
    setList = set.set
    newData = set.newData
    deleteKeys = set.deleteKeys
  }
  for (let i = 0; i < inputArray.length; i++) {
    const item = inputArray[i]
    const output = newData ? {} : item
    formatDataFromSet(item, setList, output, props)
    deleteObjKeys(output, deleteKeys)
    outputArray.push(output)
  }
  return outputArray
}

/**
 * 按照模板将数组进行分组
 * @param {*} input 输入数组
 * @param {*} set 转换模板
 * @param {*} output 输出数据
 * @param {*} props 
 * @returns 
 */
export function formatGroupFromSet(input, set, output = {}, props) {
  const groupData = output || {}
  if (!input || input.length == 0) {
    return groupData
  }
  const { keyName = 'key', groupKey, itemSet,groupNormerKeys } = set
  const newArray = itemSet ? formatArrayFromSet(input, itemSet, [], props) : input

  for (let i = 0; i < newArray.length; i++) {
    const item = input[i]
    const newItem = newArray[i]
    const keys = formatDataFromSet(item, groupKey, {}, props)
    let key = ''
    for (let j = 0; j < groupKey.length; j++) {
      if (key.length > 0) {
        key += '_'
      }
      const value = keys[groupKey[j].name]
      key += formatNullStr(value)
    }
    let groupItem = groupData[key]
    if (!groupItem) {
      let tmpData=formatDataFromSet(item, groupNormerKeys, {}, props)
      groupItem = {...tmpData, ...keys, list: [] }
      
      groupItem[keyName] = key
      groupData[key] = groupItem
    }
    const obj = { ...newItem, ...keys }
    groupItem.list.push(obj)
  }
  return groupData
}


/**
 * 按照模板将数组进行分组
 * @param {*} input 输入数组
 * @param {*} set 转换模板
 * @param {*} output 输出数据
 * @param {*} props 
 * @returns 返回数组
 */
 export function formatGroupListFromSet(input, set, output = [], props) {
  const groupData = {};
  output=output||[];
  if (!input || input.length == 0) {
    return output
  }
  const { keyName = 'key', groupKey, itemSet,groupNormerKeys } = set
  const newArray = itemSet ? formatArrayFromSet(input, itemSet, [], props) : input

  for (let i = 0; i < newArray.length; i++) {
    const item = input[i]
    const newItem = newArray[i]
    const keys = formatDataFromSet(item, groupKey, {}, props)
    let key = ''
    for (let j = 0; j < groupKey.length; j++) {
      if (key.length > 0) {
        key += '_'
      }
      const value = keys[groupKey[j].name]
      key += formatNullStr(value)
    }
    let groupItem = groupData[key]
    if (!groupItem) {
      let tmpData=formatDataFromSet(item, groupNormerKeys, {}, props)
      groupItem = {...tmpData, ...keys, list: [] }
      
      groupItem[keyName] = key
      groupData[key] = groupItem
      output.push(groupItem)
    }
    const obj = { ...newItem, ...keys }
    groupItem.list.push(obj)
  }
  return output
}